﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using log4net;
using log4net.Config;

namespace SISCON.Dao
{
    public class CommonDAO
    {
        protected static string _ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
        
        public CommonDAO() {}

        public string getReaderString(SqlDataReader reader, string columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull || o.ToString().Trim().Length == 0)
            {
                return null;
            }
            else
            {
                return reader[columnName].ToString();
            }
        }

        public List<string> getReaderCommaSeparatedString(SqlDataReader reader, string columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull || o.ToString().Trim().Length == 0)
            {
                return null;
            }
            else
            {
                return reader[columnName].ToString().Trim().Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
            }
        }

        public long getReaderLong(SqlDataReader reader, string columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull)
            {
                return 0;
            }
            else
            {
                return long.Parse(reader[columnName].ToString());
            }
        }

        public int getReaderInt(SqlDataReader reader, String columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull)
            {
                return 0;
            }
            else
            {
                return Int32.Parse(reader[columnName].ToString());
            }
        }

        public Decimal getReaderDecimal(SqlDataReader reader, String columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull)
            {
                return 0;
            }
            else
            {
                return Decimal.Parse(reader[columnName].ToString());
            }
        }

        public bool getReaderBool(SqlDataReader reader, String columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull)
            {
                return false;
            }
            else
            {
                return (bool)reader[columnName];
            }
        }

        public DateTime getReaderDateTime(SqlDataReader reader, string columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull || o.ToString().Trim().Length == 0)
            {
                return new DateTime();
            }
            else
            {
                return (DateTime)reader[columnName];
            }
        }

        public Nullable<DateTime> getReaderNullableDateTime(SqlDataReader reader, string columnName)
        {
            Object o = reader[columnName];
            if (o == null || o is System.DBNull || o.ToString().Trim().Length == 0)
            {
                return null;
            }
            else
            {
                return (DateTime)reader[columnName];
            }
        }

        public static SqlParameter getNewParameter(string parameterName, SqlDbType sqlType, int length, Object value)
        {
            SqlParameter parameter = new SqlParameter(parameterName, sqlType, length);
            parameter.Value = value != null ? value : DBNull.Value;
            return parameter;
        }

        public static SqlParameter getNewDecimalParameter(string parameterName, Object value, byte precision, byte scale)
        {
            SqlParameter parameter = new SqlParameter(parameterName, SqlDbType.Decimal, 18);
            parameter.Precision = precision;
            parameter.Scale = scale;
            parameter.Value = value != null ? value : DBNull.Value;
            return parameter;
        }
    }
}